package com.example.onlinemonitoring.comCenter;

import com.example.onlinemonitoring.Util.CRC16;
import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.Util.IEEE754Utils;
import com.example.onlinemonitoring.dao.DlqjchistoryTestMapper;
import com.example.onlinemonitoring.model.Byqpdsensor;
import com.example.onlinemonitoring.model.Dlhlsensor;
import com.example.onlinemonitoring.model.Dljfsensor;
import com.example.onlinemonitoring.model.DlqjchistoryTest;
import com.example.onlinemonitoring.model.Dlqjcsensor;
import com.example.onlinemonitoring.model.Dqjdcwsensor;
import com.example.onlinemonitoring.model.Gbjfsensor;
import com.example.onlinemonitoring.model.Gbzdsensor;
import com.example.onlinemonitoring.model.Gispdsensor;
import com.example.onlinemonitoring.model.Imsensor;
import com.example.onlinemonitoring.model.Newblqsensor;
import com.example.onlinemonitoring.model.O2andsf6sensor;
import com.example.onlinemonitoring.model.Oilsensor;
import com.example.onlinemonitoring.model.Sctmnsensor;
import com.example.onlinemonitoring.model.Sf6sensor;
import com.example.onlinemonitoring.model.Txpdsensor;
import com.example.onlinemonitoring.model.Yggxsensor;
import com.example.onlinemonitoring.model.Tgsensor;
import com.example.onlinemonitoring.model.Scpdsensor;
import com.example.onlinemonitoring.model.Dqjdcwsensor;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Arcsupcoilsensor;
import com.example.onlinemonitoring.serviceImpl.ByqpdsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.DlhlsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.DljfsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.DlqjcsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.GbjfsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.GbzdsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.GispdsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.ImsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.NewblqsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.O2andsf6sensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.OilsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.ScpdsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.SctmnsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.Sf6sensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.TxpdsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.YggxsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.TgsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.DqjdcwsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.AppdeviceServiceImpl;
import com.example.onlinemonitoring.serviceImpl.ArcsupcoilsensorServiceImpl;

import tk.mybatis.mapper.entity.Example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Scope;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * 全站数据转发，标准Modbus转发
 */
@Service("dfmodbusrtuCom")
@Scope("prototype")
public class DfmodbusrtuCom extends CommPortManager {
    String currentdeviceid = "";
    HashMap<Integer, String> devmap = new HashMap<>();
    @Autowired
    AppdeviceServiceImpl AppdeviceImpl;
    @Autowired
    private ByqpdsensorServiceImpl byqpdsensorserviceimpl;
    @Autowired
    private GispdsensorServiceImpl gispdsensorserviceimpl;
    @Autowired
    private OilsensorServiceImpl oilsensorserviceimpl;
    @Autowired
    private SctmnsensorServiceImpl sctmnsensorserviceimpl;
    @Autowired
    private ImsensorServiceImpl imsensorserviceimpl;
    @Autowired
    private YggxsensorServiceImpl yggxsensorserviceimpl;
    @Autowired
    private NewblqsensorServiceImpl newblqsensorserviceimpl;
    @Autowired
    private TxpdsensorServiceImpl txpdsensorserviceimpl;
    @Autowired
    private Sf6sensorServiceImpl sf6sensorserviceimpl;
    @Autowired
    private DlqjcsensorServiceImpl dlqserviceimpl;
    @Autowired
    private DlqjchistoryTestMapper dlqjchistorytestmapper;
    @Autowired
    private O2andsf6sensorServiceImpl o2andsf6sensorserviceimpl;
    @Autowired
    private GbzdsensorServiceImpl gbzdsensorserviceimpl;
    @Autowired
    private GbjfsensorServiceImpl gbjfsensorserviceimpl;
    @Autowired
    private DlhlsensorServiceImpl dlhlsensorserviceimpl;
    @Autowired
    private DljfsensorServiceImpl dljfsensorserviceimpl;
    @Autowired
    private TgsensorServiceImpl tgsensorserviceimpl;
    @Autowired
    private ScpdsensorServiceImpl scpdsensorServiceImpl;
    @Autowired
    private DqjdcwsensorServiceImpl dqjdcwsensorServiceImpl;
    @Autowired
    private ArcsupcoilsensorServiceImpl arcsupcoilsensorServiceImpl;

    List<String> devstr = new ArrayList<>();
    public String checkType = "crc16";

    private HashMap<Integer, Integer> devidmap = new HashMap<>();
    private HashMap<Integer, String> devgroupmap = new HashMap<>();

    /*
     * 1、变压器局放
     * 2、GIS局放
     * 3、油色谱
     * 4、铁芯接地
     * 5、开关柜测温
     * 6、荧光光纤
     * 7、避雷器
     * 8、微水
     * 9、铁芯接地(智能监控器)
     * 10、断路器常态数据
     * 11、断路器谱图
     * 12、环境监控
     * 13、干变振动
     * 14、干变局放
     * 15、电缆环流
     * 16、电缆局放
     * 17、套管
     * 18、开关柜局放
     * 19、电气接点
     */

    String[] devsimple = { "byqpd", "gispd", "oil", "txpd", "sctmn", "yggx", "newblq", "sf6", "im", "dlqjc", "dlqjc",
            "o2andsf6", "gbzd", "gbjf", "dlhl", "dljf", "tg", "scpd", "dqjdcw" };

    // 1|1-4,3|1-1,2|1-1,4|1-1,5|1-1,6|1-1,7|1-1,8|1-1,9|1-1,10|1-1,11|1-1,12|1-1,13|1-1,14|1-1,15|1-1,16|1-1,17|1-1,18|1-1,19|1-1
    @Override
    protected void myInit() {
        if (ad != null && ad.getSubswitch() != null && !ad.getSubswitch().equals("")) {
            String[] subswitch = ad.getSubswitch().split(",");// 1|1-4
            for (int i = 0; i < devsimple.length; i++) {
                if (i >= subswitch.length) {
                    break;
                }
                String[] sub = subswitch[i].split("\\|");

                Example example = new Example(Appdevice.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("stationid", ad.getStationid());
                // criteria.andEqualTo("installed", 1);
                criteria.andEqualTo("simple", devsimple[i]);
                if (AppdeviceImpl.selectCountByExample(example) == 0) {
                    continue;
                }

                devidmap.put(Integer.parseInt(sub[0]), i);
                devgroupmap.put(Integer.parseInt(sub[0]), sub[1]);// 1-4
            }
        }
    }

    @Override
    @Transactional
    public void receiveData(String str, OutputStream out) {
        try {
            str = str.toUpperCase();
            setText(str);
            switch (str.substring(2, 4)) {
                case "03": {// 010300020007A5C8
                    resolveData(str, out);
                }
                    break;
                case "04": {
                    resolveData2(str, out);
                }
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void resolveData(String str, OutputStream out) {
        Integer devid = parseHex4(str.substring(0, 2));
        if (devgroupmap.get(devid) == null || devidmap.get(devid) == null) {
            return;
        }
        Integer groupstart = Integer.parseInt(devgroupmap.get(devid).split("-")[0]);
        Integer groupend = Integer.parseInt(devgroupmap.get(devid).split("-")[1]);
        Example example = new Example(Appdevice.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("stationid", ad.getStationid());
        // criteria.andEqualTo("installed", 1);
        criteria.andEqualTo("simple", devsimple[devidmap.get(devid)]);
        if (AppdeviceImpl.selectCountByExample(example) == 0) {
            return;
        }
        switch (devidmap.get(devid)) {
            case 0:// 变压器局放
                setText("开始巡检：变压器局放");
                resolveByqpd(str, groupstart, groupend, out);
                break;
            case 1:// GIS局放
                setText("开始巡检：GIS局放");
                resolveGispd(str, groupstart, groupend, out);
                break;
            case 2:// 油色谱
                setText("开始巡检：油色谱");
                resolveOil(str, groupstart, groupend, out);
                break;
            case 3:// 铁芯
                setText("开始巡检：铁芯");
                resolveTxpd(str, groupstart, groupend, out);
                break;
            case 4:// 开关柜
                setText("开始巡检：开关柜");
                resolveSctm(str, groupstart, groupend, out);
                break;
            case 5:// 荧光光纤
                setText("开始巡检：荧光光纤");
                resolveYggx(str, groupstart, groupend, out);
                break;
            case 6:// 避雷器
                setText("开始巡检：避雷器");
                resolveBlq(str, groupstart, groupend, out);
                break;
            case 7:// 微水
                setText("开始巡检：微水");
                resolveSf6(str, groupstart, groupend, out);
                break;
            case 8:// 铁芯（智能监控器）
                setText("开始巡检：铁芯（智能监控器）");
                resolveTxpdIm(str, groupstart, groupend, out);
                break;
            case 9:// 断路器常态数据
                setText("开始巡检：断路器常态数据");
                resolveDlqNormal(str, groupstart, groupend, out);
                break;
            case 10:// 断路器波形数据
                setText("开始巡检：断路器波形数据");
                resolveDlq(str, groupstart, groupend, out);
                break;
            case 11:// 环境监控
                setText("开始巡检：环境监控");
                resolveHjjk(str, groupstart, groupend, out);
                break;
            case 12:// 干变振动
                setText("开始巡检：干变振动");
                resolveGbzd(str, groupstart, groupend, out);
                break;
            case 13:// 干变局放
                setText("开始巡检：干变局放");
                resolveGbjf(str, groupstart, groupend, out);
                break;
            case 14:// 电缆环流
                setText("开始巡检：电缆环流");
                resolveDlhl(str, groupstart, groupend, out);
                break;
            case 15:// 电缆局放
                setText("开始巡检：电缆局放");
                resolveDljf(str, groupstart, groupend, out);
                break;
            case 16:// 套管
                setText("开始巡检：套管");
                resolveTg(str, groupstart, groupend, out);
                break;
            case 17:// 开关柜局放
                setText("开始巡检：开关柜局放");
                resolveScpd(str, groupstart, groupend, out);
                break;
            case 18:// 电气接点
                setText("开始巡检：电气接点测温");
                resolveDqjdcw(str, groupstart, groupend, out);
                break;
            case 111111:// 变压器中性点直流装置（智能监控器）
                // resolveIm(str, groupstart, groupend, out);
                break;
            case 111112:// 消弧线圈控制器
                setText("开始巡检：消弧线圈控制器");
                resolveXh(str, groupstart, groupend, out);
                break;
            default:
                break;
        }
    }

    private void resolveData2(String str, OutputStream out) {
        setText("开始巡检：油色谱");
        resolveOil2(str, 1, 1, out);
    }

    private void resolveDlqNormal(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Dlqjcsensor record = new Dlqjcsensor();
        record.setStationid(ad.getStationid());

        record.setCmd(67);
        List<Dlqjcsensor> list = dlqserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            devmap.put(index++, String.format("%04x", isnull(record.getLastastoragestate())));
            devmap.put(index++, String.format("%04x", isnull(record.getLastastate())));
            devmap.put(index++,
                    String.format("%04x",
                            (short) (Math.abs(isnull(record.getLastaacurrent()) - isnull(record.getLastaccurrent()))
                                    * 10)));
            devmap.put(index++, String.format("%04x", (short) (isnull(record.getLastaacurrent()) * 10)));
            devmap.put(index++, String.format("%04x", isnull(record.getAcount())));
            devmap.put(index++, String.format("%04x", (short) ((100 - isnull(record.getLifetime())) * 10)));
            Dlqjcsensor dlq = new Dlqjcsensor();
            {
                Example example = new Example(Dlqjcsensor.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("stationid", ad.getStationid());
                criteria.andEqualTo("devicegroup", ad.getDevicegroup());
                criteria.andEqualTo("deviceaddr", parseHex4(str.substring(2, 4)) + 6010);
                criteria.andEqualTo("cmd", 64);
                criteria.andEqualTo("laststate", 2);
                dlq = dlqserviceimpl.selectOneByExample(example);
                devmap.put(index++, String.format("%04x", (short) (isnull(dlq.getLastspeed()) * 10)));
            }
            {
                Example example = new Example(Dlqjcsensor.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("stationid", ad.getStationid());
                criteria.andEqualTo("devicegroup", ad.getDevicegroup());
                criteria.andEqualTo("deviceaddr", parseHex4(str.substring(2, 4)) + 6010);
                criteria.andEqualTo("cmd", 64);
                criteria.andEqualTo("laststate", 1);
                dlq = dlqserviceimpl.selectOneByExample(example);
                devmap.put(index++, String.format("%04x", (short) (isnull(dlq.getLastspeed()) * 10)));
            }
        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", isnull(record.getLastalarmmessage()).contains("卡涩"))
                    + String.format("%02x", isnull(record.getLastalarmmessage()).contains("卡滞")));
            devmap.put(index++, isnull(record.getLastalarmmessage()).contains("偏离") ? "0100" : "0000");
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveDlq(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        DlqjchistoryTest record = new DlqjchistoryTest();
        Example example1 = new Example(DlqjchistoryTest.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("stationid", ad.getStationid());
        criteria1.andBetween("devicegroup", groupstart, groupend);
        example1.orderBy("datatime").asc();
        List<DlqjchistoryTest> list = dlqjchistorytestmapper.selectByExample(example1);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%04x", record.getDeviceaddr() - 6010));
            devmap.put(index++, String.format("%04x", (short) (1.2 * 1000)));
            devmap.put(index++, String.format("%04x", (short) (1.001 * 1000)));
            devmap.put(index++, String.format("%04x", 0));
            devmap.put(index++, Converttime1(record.getDatatime()));
            devmap.put(index++, Converttime2(record.getDatatime()));
            devmap.put(index++, String.format("%04x", 0));
            byte[] bt;
            String chstr = "";
            try {
                bt = record.getPointname().getBytes("gbk");
                for (int j = 0; j < 32; j++) {
                    if (bt.length > j) {
                        chstr += String.format("%02x", bt[j]);
                    } else {
                        chstr += "00";
                    }
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            for (int j = 0; j < 64; j += 4) {
                devmap.put(index++, chstr.substring(j, j + 4));
            }
            devmap.put(index++, "0002");
            if (record.getLaststate() == 3 || record.getLaststate() == 4) {
                devmap.put(index++, String.format("%04x", (short) (3 * 100)));
            } else {
                devmap.put(index++, String.format("%04x", (short) (1 * 100)));
            }
            devmap.put(index++, String.format("%04x", (short) (0.1 * 100)));
            devmap.put(index++, String.format("%04x", 0));
            devmap.put(index++, String.format("%04x", 0));
            devmap.put(index++, String.format("%04x", 100));

            String[] cmdtype = { "0001", "0002", "0003" };
            String[] statetype = { "0000", "0001", "0002", "0003", "0004" };
            devmap.put(index++, cmdtype[record.getCmd() - 64]);
            devmap.put(index++, statetype[record.getLaststate()]);
            devmap.put(index++, String.format("%08x", record.getId().intValue()).substring(0, 4));
            devmap.put(index++, String.format("%08x", record.getId().intValue()).substring(4, 8));
            float[] x = new float[6];
            float[] y = new float[6];
            x[0] = record.getT0time();
            x[1] = record.getT1time();
            x[2] = record.getT2time();
            x[3] = record.getT3time();
            x[4] = record.getT4time();
            x[5] = record.getT5time();
            y[0] = record.getI0current();
            y[1] = record.getI1current();
            y[2] = record.getI2current();
            y[3] = record.getI3current();
            y[4] = record.getI4current();
            y[5] = record.getI5current();
            float[][] xy = { { x[0], y[0] }, { x[1], y[1] }, { x[2], y[2] }, { x[3], y[3] }, { x[4], y[4] },
                    { x[5], y[5] } };
            float[][] res = bezier(xy, 100);
            for (int j = 0; j < 100; j++) {
                Float f = res[j][1] * 10;
                devmap.put(index++, String.format("%04x", f.shortValue()));
            }
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%04x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveSf6(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Sf6sensor record = new Sf6sensor();
        record.setStationid(ad.getStationid());

        List<Sf6sensor> list = sf6sensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getLasttemperature()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastpressure()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastdensity()) * 10)));
            if (isnull(record.getLastppm()) > 655.35) {
                devmap.put(index++, "FFFF");
            } else {
                devmap.put(index++,
                        String.format("%04x", (short) Math.round(isnull(record.getLastppm()) * 10) & 0xFFFF));
            }
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastchangya()) * 10)));
        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveBlq(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Newblqsensor record = new Newblqsensor();
        record.setStationid(ad.getStationid());

        List<Newblqsensor> list = newblqsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        Random rd = new Random();
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);

            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            devmap.put(index++, String.format("%04x", isnull(record.getDeviceaddr())));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastvref()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastacurrent()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastbcurrent()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastccurrent()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastarcurrent()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastbrcurrent()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastcrcurrent()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastaangle()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastbangle()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastcangle()) * 100)));
            devmap.put(index++, Converttime1(record.getLastthundertime()));
            devmap.put(index++, Converttime2(record.getLastthundertime()));
            devmap.put(index++, Converttime1(record.getLastthundertimeb()));
            devmap.put(index++, Converttime2(record.getLastthundertimeb()));
            devmap.put(index++, Converttime1(record.getLastthundertimec()));
            devmap.put(index++, Converttime2(record.getLastthundertimec()));
            devmap.put(index++, String.format("%04x", isnull(record.getLastthundercount())));
            devmap.put(index++, String.format("%04x", isnull(record.getLastthundercountb())));
            devmap.put(index++, String.format("%04x", isnull(record.getLastthundercountc())));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getLastavacurrent()) * 100)));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getLastavbcurrent()) * 100)));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getLastavccurrent()) * 100)));
            devmap.put(index++, String.format("%04x", Math.round(rd.nextDouble() * 100)));
            devmap.put(index++, String.format("%04x", Math.round(rd.nextDouble() * 100)));
            devmap.put(index++, String.format("%04x", Math.round(rd.nextDouble() * 100)));
            devmap.put(index++, String.format("%04x", isnull(record.getPtaddr())));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getSimfrequence()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getAsimxbvoltage()) * 100)));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getAsimxbvoltage1()) * 100)));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getAsimxbvoltage2()) * 100)));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getAsimxbvoltage3()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getBsimxbvoltage()) * 100)));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getBsimxbvoltage1()) * 100)));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getBsimxbvoltage2()) * 100)));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getBsimxbvoltage3()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getCsimxbvoltage()) * 100)));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getCsimxbvoltage1()) * 100)));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getCsimxbvoltage2()) * 100)));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getCsimxbvoltage3()) * 100)));

            devmap.put(index++, String.format("%04x", isnull(record.getWsdaddr())));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getLasttemperature()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasthumidity()) * 100)));

        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", isWarn(record.getLastawcurrentalarmstatus()))
                    + String.format("%02x", isWarn(record.getLastbwcurrentalarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastcwcurrentalarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastawcurrentalarmstatus())));
            devmap.put(index++, String.format("%02x", isAlarm(record.getLastbwcurrentalarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastcwcurrentalarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastarcurrentalarmstatus()))
                    + String.format("%02x", isWarn(record.getLastbrcurrentalarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastcrcurrentalarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastarcurrentalarmstatus())));
            devmap.put(index++, String.format("%02x", isAlarm(record.getLastbrcurrentalarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastcrcurrentalarmstatus())));
            devmap.put(index++, String.format("%02x", convertStatus(record.getLastcommunicationstatusBlq()))
                    + String.format("%02x", convertStatus(record.getLastalarmlevelBlq())));
            devmap.put(index++, String.format("%02x", convertStatus(record.getLastcommunicationstatusPt()))
                    + String.format("%02x", convertStatus(record.getLastalarmlevelPt())));
            devmap.put(index++, String.format("%02x", convertStatus(record.getLastcommunicationstatusWsd()))
                    + String.format("%02x", convertStatus(record.getLastalarmlevelWsd())));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private String Converttime1(Date lastdatatime) {
        if (lastdatatime == null) {
            return "0000";
        }
        try {
            return String.format("%08x", lastdatatime.getTime() / 1000).substring(0, 4);
        } catch (Exception e) {
            return "0000";
        }
    }

    private String Converttime2(Date lastdatatime) {
        if (lastdatatime == null) {
            return "0000";
        }
        try {
            return String.format("%08x", lastdatatime.getTime() / 1000).substring(4, 8);
        } catch (Exception e) {
            return "0000";
        }
    }

    private void resolveYggx(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Yggxsensor record = new Yggxsensor();
        record.setStationid(ad.getStationid());

        List<Yggxsensor> list = yggxsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasttemper()) * 10)));
        }
        index = 3001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveSctm(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Sctmnsensor record = new Sctmnsensor();
        record.setStationid(ad.getStationid());

        List<Sctmnsensor> list = sctmnsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            Float tmps[] = { record.getLasta(), record.getLastb(), record.getLastc() };
            for (int j = 0; j < tmps.length; j++) {
                devmap.put(index++, String.format("%04x", (short) Math.round(isnull(tmps[j]) * 10)));
            }
        }
        index = 3001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            Float tmps[] = { record.getLasta(), record.getLastb(), record.getLastc() };
            for (int j = 0; j < tmps.length; j++) {
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
            }
        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            Float tmps[] = { record.getLasta(), record.getLastb(), record.getLastc() };
            for (int j = 0; j < tmps.length; j++) {
                devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                        + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
            }
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        setText(s);
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveGispd(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Gispdsensor record = new Gispdsensor();
        record.setStationid(ad.getStationid());

        List<Gispdsensor> list = gispdsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            switch (isnull(record.getLastdischargetype())) {
                case "颗粒放电":
                    devmap.put(index++, String.format("%04x", 0));
                    break;
                case "间隙放电":
                    devmap.put(index++, String.format("%04x", 1));
                    break;
                case "电晕放电":
                    devmap.put(index++, String.format("%04x", 2));
                    break;
                case "电极放电":
                    devmap.put(index++, String.format("%04x", 3));
                    break;
                case "空穴放电":
                    devmap.put(index++, String.format("%04x", 4));
                    break;
                case "未知":
                    devmap.put(index++, String.format("%04x", 5));
                    break;
                default:
                    devmap.put(index++, String.format("%04x", 0));
                    break;
            }
            devmap.put(index++, String.format("%04x", isnull(record.getLastrst())));
            devmap.put(index++, String.format("%08x", isnull(record.getLasttot())).substring(0, 4));
            devmap.put(index++, String.format("%08x", isnull(record.getLasttot())).substring(4, 8));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getLastdischargeamount()) * 100)));
            devmap.put(index++, String.format("%04x", isnull(record.getLastsignalpower())));
            devmap.put(index++, String.format("%04x", isnull(record.getLasthavesignal())));

        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }

        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveTxpdIm(String str, int groupstart, int groupend, OutputStream out) {// 010300650020540D
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Imsensor record = new Imsensor();
        record.setStationid(ad.getStationid());

        List<Imsensor> list = imsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            if (record.getPointaddr() == 1) {
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++,
                        String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(0, 4));
                devmap.put(index++,
                        String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(4, 8));
            }
            if (record.getPointaddr() == 2) {
                devmap.put(index++,
                        String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(0, 4));
                devmap.put(index++,
                        String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(4, 8));
            }
        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            if (record.getPointaddr() == 1) {
                devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                        + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
            }
            if (record.getPointaddr() == 2) {
                devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                        + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
            }
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveTxpd(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Txpdsensor record = new Txpdsensor();
        record.setStationid(ad.getStationid());

        List<Txpdsensor> list = txpdsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;

        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);

            if (record.getPointaddr() == 9) {
                devmap.put(index++, Converttime1(record.getLastdatatime()));
                devmap.put(index++, Converttime2(record.getLastdatatime()));
                devmap.put(index++,
                        String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(0, 4));
                devmap.put(index++,
                        String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(4, 8));
            }
            if (record.getPointaddr() == 10) {
                devmap.put(index++,
                        String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(0, 4));
                devmap.put(index++,
                        String.format("%08x", Math.round(isnull(record.getLastdata()) * 100)).substring(4, 8));
            }
        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);

            if (record.getPointaddr() == 9) {
                devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                        + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
            }
            if (record.getPointaddr() == 10) {
                devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                        + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
            }
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveOil(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Oilsensor record = new Oilsensor();
        record.setStationid(ad.getStationid());

        List<Oilsensor> list = oilsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));

            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastch4()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc2h6()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc2h4()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastc2h2()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastco()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastco2()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasto2()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastn2()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasth2()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastppm()) * 10)));
            devmap.put(index++, "0000");
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasttothyd()) * 10)));

        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", isWarn(record.getLastch4alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastch4alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastc2h6alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastc2h6alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastc2h4alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastc2h4alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastc2h2alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastc2h2alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastcoalarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastcoalarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastco2alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastco2alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLasto2alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLasto2alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastn2alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastn2alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLasth2alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLasth2alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastppmalarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastppmalarmstatus())));
            devmap.put(index++, "0000");
            devmap.put(index++, String.format("%02x", isWarn(record.getLasttothydalarmstatus()))
                    + String.format("%02x", isAlarm(record.getLasttothydalarmstatus())));
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    /**
     * 台电项目需要特定寄存器返回
     * 
     * @param str
     * @param groupstart
     * @param groupend
     * @param out
     */
    private void resolveOil2(String str, int groupstart, int groupend, OutputStream out) {
        // 040400C80016 , 040400E60005
        int deviceAddr = parseHex4(str.substring(0, 2));
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Oilsensor record = new Oilsensor();
        record.setStationid(ad.getStationid());
        if (deviceAddr == 1) {
            record.setDeviceaddr(40);
        }
        if (deviceAddr == 2) {
            record.setDeviceaddr(46);
        }
        if (deviceAddr == 3) {
            record.setDeviceaddr(41);
        }
        if (deviceAddr == 4) {
            record.setDeviceaddr(50);
        }
        if (deviceAddr == 5) {
            record.setDeviceaddr(42);
        }
        if (deviceAddr == 6) {
            record.setDeviceaddr(43);
        }
        List<Oilsensor> list = oilsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 200;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));

            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLastch4())).substring(0, 4));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLastch4())).substring(4, 8));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLastc2h4())).substring(0, 4));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLastc2h4())).substring(4, 8));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLastc2h6())).substring(0, 4));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLastc2h6())).substring(4, 8));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLastc2h2())).substring(0, 4));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLastc2h2())).substring(4, 8));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLasth2())).substring(0, 4));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLasth2())).substring(4, 8));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLastco())).substring(0, 4));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLastco())).substring(4, 8));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLastco2())).substring(0, 4));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLastco2())).substring(4, 8));
            devmap.put(index++, "0000");
            devmap.put(index++, "0000");
            devmap.put(index++, "0000");
            devmap.put(index++, "0000");
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLasttothyd())).substring(0, 4));
            devmap.put(index++, IEEE754Utils.singleToHex(isnull(record.getLasttothyd())).substring(4, 8));
        }
        index = 230;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", isWarn(record.getLasth2alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLasth2alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastch4alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastch4alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastc2h6alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastc2h6alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastc2h4alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastc2h4alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastc2h2alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastc2h2alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastcoalarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastcoalarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastco2alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastco2alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLasto2alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLasto2alarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLastn2alarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastn2alarmstatus())));

            devmap.put(index++, String.format("%02x", isWarn(record.getLastppmalarmstatus()))
                    + String.format("%02x", isAlarm(record.getLastppmalarmstatus())));
            devmap.put(index++, String.format("%02x", isWarn(record.getLasttothydalarmstatus()))
                    + String.format("%02x", isAlarm(record.getLasttothydalarmstatus())));
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveByqpd(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Byqpdsensor record = new Byqpdsensor();
        record.setStationid(ad.getStationid());
        List<Byqpdsensor> list = byqpdsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            switch (isnull(record.getLastdischargetype())) {
                case "颗粒放电":
                    devmap.put(index++, String.format("%04x", 0));
                    break;
                case "间隙放电":
                    devmap.put(index++, String.format("%04x", 1));
                    break;
                case "电晕放电":
                    devmap.put(index++, String.format("%04x", 2));
                    break;
                case "电极放电":
                    devmap.put(index++, String.format("%04x", 3));
                    break;
                case "空穴放电":
                    devmap.put(index++, String.format("%04x", 4));
                    break;
                case "未知":
                    devmap.put(index++, String.format("%04x", 5));
                    break;
                default:
                    devmap.put(index++, String.format("%04x", 0));
                    break;
            }
            devmap.put(index++, String.format("%04x", isnull(record.getLastrst())));
            devmap.put(index++, String.format("%08x", isnull(record.getLasttot())).substring(0, 4));
            devmap.put(index++, String.format("%08x", isnull(record.getLasttot())).substring(4, 8));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getLastdischargeamount()) * 100)));
            devmap.put(index++, String.format("%04x", isnull(record.getLastsignalpower())));
            devmap.put(index++, String.format("%04x", isnull(record.getLasthavesignal())));

        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveHjjk(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        O2andsf6sensor record = new O2andsf6sensor();
        record.setStationid(ad.getStationid());

        List<O2andsf6sensor> list = o2andsf6sensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasttemperature()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasthumidity()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastsf6()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLasto2consistence()) * 10)));
        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastsf6alarmstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLasto2alarmstatus()))));
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveGbzd(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Gbzdsensor record = new Gbzdsensor();
        record.setStationid(ad.getStationid());

        List<Gbzdsensor> list = gbzdsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastdata()) * 10)));
        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveGbjf(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Gbjfsensor record = new Gbjfsensor();
        record.setStationid(ad.getStationid());

        List<Gbjfsensor> list = gbjfsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            devmap.put(index++,
                    String.format("%04x", (short) Math.round(isnull(record.getLastelectricity()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastamplitude()) * 10)));

        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveDlhl(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Dlhlsensor record = new Dlhlsensor();
        record.setStationid(ad.getStationid());

        List<Dlhlsensor> list = dlhlsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastacurrent()) * 10)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastbcurrent()) * 10)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastccurrent()) * 10)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastdcurrent()) * 10)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLasta1current()) * 10)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastb1current()) * 10)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastc1current()) * 10)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastd1current()) * 10)));
        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveDljf(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Dljfsensor record = new Dljfsensor();
        record.setStationid(ad.getStationid());

        List<Dljfsensor> list = dljfsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastadischarge()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastareq()) * 10)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastaphase()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLastbdischarge()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLastbreq()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLastbphase()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLastcdischarge()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLastcreq()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLastcphase()) * 10)));

            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLasta1discharge()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLasta1req()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLasta1phase()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLastb1discharge()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLastb1req()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLastb1phase()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLastc1discharge()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLastc1req()) * 10)));
            // devmap.put(index++, String.format("%04x", (short)
            // Math.round(isnull(record.getLastc1phase()) * 10)));
        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveTg(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Tgsensor record = new Tgsensor();
        record.setStationid(ad.getStationid());

        List<Tgsensor> list = tgsensorserviceimpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastawcurrent()) * 100)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastbwcurrent()) * 100)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastcwcurrent()) * 100)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLasttandeltaa()) * 100)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLasttandeltab()) * 100)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLasttandeltac()) * 100)));

        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastawalarmstatus()))));
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastbwalarmstatus()))));
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastcwalarmstatus()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveScpd(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Scpdsensor record = new Scpdsensor();
        record.setStationid(ad.getStationid());

        List<Scpdsensor> list = scpdsensorServiceImpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastbatteryamount()) * 100)));
            devmap.put(index++, String.format("%04x", (short) (isnull(record.getLastaedischarge()) * 100)));
            devmap.put(index++, String.format("%04x", (short) (isnull(record.getLasteffaedischarge()) * 100)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLasttevvol()) * 100)));
        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus())))
                    + String.format("%02x", convertStatus(isnull(record.getLastalarmlevel()))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveDqjdcw(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Dqjdcwsensor record = new Dqjdcwsensor();
        record.setStationid(ad.getStationid());

        List<Dqjdcwsensor> list = dqjdcwsensorServiceImpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastatemper()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastbtemper()) * 100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastctemper()) * 100)));
            // devmap.put(index++, String.format("%04x",
            // (short)Math.round(isnull(record.getLastdtemper())*100)));
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastv()))));
            devmap.put(index++, String.format("%08x", Math.round(isnull(record.getLastcount()))));
        }
        index = 10001;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, String.format("%04x", (short) Math.round(isnull(record.getLastsbalarmstatus()))));
            devmap.put(index++, String.format("%02x", convertStatus(isnull(record.getLastalarmlevel())))
                    + String.format("%02x", convertStatus(isnull(record.getLastcommunicationstatus()))));
            // devmap.put(index++, String.format("%02x",
            // convertStatus(isnull(record.getLastcommunicationstatus())))
            // + String.format("%02x",
            // convertStatus(isnull(record.getlasttevdischargestatus()))));
            // devmap.put(index++, String.format("%02x",
            // convertStatus(isnull(record.getLastalarmlevel())))
            // + String.format("%02x", convertStatus(isnull(0))));
        }
        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private void resolveXh(String str, int groupstart, int groupend, OutputStream out) {
        int startAddr = parseHex4(str.substring(4, 8));
        int addrLength = parseHex4(str.substring(8, 12));
        devmap.clear();
        /*--------------- 这一段自定义代码 --------------- */
        Arcsupcoilsensor record = new Arcsupcoilsensor();
        record.setStationid(ad.getStationid());

        List<Arcsupcoilsensor> list = arcsupcoilsensorServiceImpl.getLists(record, groupstart, groupend);
        int index = 101;
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            devmap.put(index++, Converttime1(record.getLastdatatime()));
            devmap.put(index++, Converttime2(record.getLastdatatime()));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastvref()) * 100)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastcurrent()) * 100)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastcapacity()) * 100)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastcapcurrent()) * 100)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLasttdegree()) * 100)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastrescurrent()) * 100)));
            devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastgear()))));

        }
        for (int i = 0; i < list.size(); i++) {
            record = list.get(i);
            // devmap.put(index++, String.format("%02x",
            // convertStatus(isnull(record.getLastcoilstatus())))
            // + String.format("%02x", convertStatus(isnull(record.getLastline()))));
        }

        String head = str.substring(0, 4);
        StringBuilder sb = new StringBuilder();
        for (int i = startAddr; i < startAddr + addrLength; i++) {
            if (devmap.get(i) == null) {
                sb.append("0000");
            } else {
                sb.append(devmap.get(i));
            }
        }
        String s = head + String.format("%02x", sb.length() / 2) + sb.toString();
        s += CRC16.calcCrc16(s);
        Send(s, out);
        setText("返回数据：" + s + "\r\n");
    }

    private Integer convertStatus(Integer status) {
        if (status == null || status == 0)
            return 0;
        else
            return 1;
    }

    private Integer isAlarm(Integer status) {
        if (status == null) {
            return 0;
        }
        if (status == 2)
            return 2;
        else
            return 0;
    }

    private Integer isWarn(Integer status) {
        if (status == null) {
            return 0;
        }
        if (status == 1)
            return 1;
        else
            return 0;
    }
}
